package com.ats.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ats.dao.ClasseDao_xh;
import com.ats.model.Classe;
import com.ats.model.ClasseRecord;
import com.ats.model.JobState;
import com.ats.model.Student;
import com.ats.util.JDBCUtil;

import net.sf.json.JSONArray;

public class ClasseDaoImpl_xh implements ClasseDao_xh{

	@Override
	public JSONArray findClasse() {
		// TODO Auto-generated method stub
		JSONArray jso = new JSONArray();		
		StringBuffer sql = new StringBuffer();
		sql.append(" select cid,cname from ats_classe ");
		Connection con =JDBCUtil.getConnection();
		PreparedStatement pst=null;
		try {
			pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){	
				Classe cl =new Classe();
				cl.setCid(rs.getInt("cid"));
				cl.setCname(rs.getString("cname"));
				jso.add(cl);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jso;
	}

	@Override
	public List<ClasseRecord> findSidByCid(int cid) {
		// TODO Auto-generated method stub
		List<ClasseRecord> lcr = new ArrayList<ClasseRecord>();
		StringBuffer  sql = new StringBuffer();
		sql.append("select ats_classe_record.sid from ats_classe_record ");
		sql.append(" inner join ats_student ");
		sql.append(" on ats_classe_record.sid=ats_student.sid ");
		sql.append(" where cid =?");
		Connection con =JDBCUtil.getConnection();
		PreparedStatement pst =null;
		try {
			pst = con.prepareStatement(sql.toString());
			pst.setInt(1, cid);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				ClasseRecord cr =new ClasseRecord();
				cr.setSid(rs.getInt("sid"));
				lcr.add(cr);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return lcr;
	}

}
